package com.apes.crm.callCenter.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.crm.callCenter.repository.CommonForCallCenterRepository;
import com.apes.crm.callCenter.repository.CustomComplainRepository;
import com.apes.crm.customerCar.model.CustomerCar;
import com.apes.crm.customerCar.repository.CustomerCarRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.party.repository.PersonRepository;
import com.apes.scm.rbac.model.Post;
import com.apes.scm.rbac.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：呼叫中心公共查询
 *
 * @author Biebi
 * @create 2018-9-11 15:11:
 */
@Service("crmCommonForCallCenterService")
public class CommonForCallCenterService extends DomainService {

    @Autowired
    CommonForCallCenterRepository commonForCallCenterRepository;

    @Autowired
    CustomComplainRepository customComplainRepository;

    @Autowired
    PersonRepository personRepository;
    @Autowired
    PartyRoleRepository partyRoleRepository;
    @Autowired
    CustomerCarRepository customerCarRepository;
    @Autowired
    PostRepository postRepository;



    /***
     * 用于查询客户信息
     * @param request
     * @return
     */
    public Map getCustomInfo(SimpleRequest request) {
        String id = request.get("id");
        if( id == null ) throw new RuntimeException("客户编码不能为空");
        PartyRole partyRole = partyRoleRepository.findOne(id);
        Map map = new HashMap();
        map.put("mobileNumber", partyRole.queryMobileNumber());
        return map;
    }




    /***
     * 功能: 查询车辆信息
     * @param request
     * @return
     */
    public Map getCarInfo(SimpleRequest request) {
        String id = request.get("id");
        if( id == null ) throw new RuntimeException("车辆编码不能为空");
        CustomerCar car = customerCarRepository.findOne(id);
        Map map = new HashMap();

        map.put("vip", car.queryCustomer());
        map.put("carConfiguration",car.queryCarConfiguration());
        map.put("carModel", car.queryCarModel());
        map.put("carLabel", car.queryCarLabel());

        return map;
    }


    /***
     * 功能： 从老框架查询
     */

    public Map findWithAuthority(SimpleRequest request) {

        JSONObject param = request.getJO();
        String CPHM;
        if( param.get("key") != null ) {
            CPHM = param.getString("key");
        } else {
            CPHM = param.getJSONArray("condition").getJSONObject(0).getString("value");
        }
        Post post = postRepository.findOne(request.getPostId());

        Map returnMap = new HashMap();

        // 获取远程数据的结果
        JSONObject jsonObject = new JSONObject(MapUtil.mapper("CPHM", CPHM, "GSID", post.getCompany().getId()));

        Map map = MapUtil.mapper(
                "pdata", jsonObject.toJSONString(),
                "interfaceMethod", "ipmVipService.checkVipBelong");

        JSONObject resultJson = JSONObject.parseObject(this.invoke("report.interface", map));

        if( resultJson != null && resultJson.getBoolean("success") ){
            JSONObject content = JSONObject.parseObject(resultJson.getString("obj"));
            if( content.getBoolean("belong") ){
                CustomerCar customerCar =  customerCarRepository.findFirstByPlateNumberAndCarFrostStatusCode(CPHM, "1");
                List customerCarList = new ArrayList();
                customerCarList.add(customerCar);
                returnMap.put("content",customerCarList);
                returnMap.put("page",1);
                returnMap.put("pages",1);
                returnMap.put("elements",customerCarList.size());
                returnMap.put("size",10);
                returnMap.put("total",customerCarList.size());
                return returnMap;
            }
        }

        returnMap.put("content", new ArrayList());
        returnMap.put("page",0);
        returnMap.put("pages",0);
        returnMap.put("elements",0);
        returnMap.put("size",10);
        returnMap.put("total",0);



        return returnMap;
    }


}
